package com.xy.type7;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date 2022/6/9 19:46
 * @description 单例模式 枚举
 */
public class SingletonTest07 {
    public static void main(String[] args) {
        Singleton test1 = Singleton.TEST1;
        Singleton test3 = Singleton.TEST1;
        Singleton test2 = Singleton.TEST2;
        System.out.println(test1 == test3);
    }
}

/**
 * 使用枚举实现单例，避免多线程，而且防止序列化问题
 */
enum Singleton {
    TEST1(1,"test1"),
    TEST2(2,"test2");

    private final Integer code;
    private final String msg;

    Singleton(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}